package com.lwj.liwanjia.view.activity.supermarket;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.MainActivity;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.LeftListAdapter;
import com.lwj.liwanjia.adapter.SuperMaketAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.MarketFenleiBean;
import com.lwj.liwanjia.bean.SuperMarketBean;
import com.lwj.liwanjia.impl.MarketleftView;
import com.lwj.liwanjia.impl.SuperMarkeView;
import com.lwj.liwanjia.presenter.MarketPresenter;
import com.lwj.liwanjia.presenter.SuperMarketPresenter;
import com.lwj.liwanjia.utils.NetWorkUtil;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.search.SearchActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 利万嘉超市
 */

public class SuperMarketActivity extends BaseActivity implements MarketleftView, SuperMarkeView, SuperRecyclerView.LoadingListener {

    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.market_search)
    RelativeLayout marketSearch;
    @BindView(R.id.gouwuche_number)
    TextView gouwucheNumber;
    @BindView(R.id.left_listview)
    ListView leftListview;
    @BindView(R.id.market_rlv)
    SuperRecyclerView marketRlv;
    @BindView(R.id.tv_haoping)
    TextView tvHaoping;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.iv_no_dianpu)
    ImageView ivNoDianpu;

    private List<SuperMarketBean.DataBean.MarketBean> list = new ArrayList<>();
    private SuperMaketAdapter marketAdapter;
    private LeftListAdapter adapter;
    private List<String> leftList = new ArrayList<>();//左侧超市
    private List<Boolean> flagArray = new ArrayList<>();//左侧选中
    private MarketPresenter marketPresenter;
    private SuperMarketPresenter superMarketPresenter;//好评度排序
    private Map<String, Object> map = new HashMap<>();
    private SpfUtils utils;
    private int page = 0;
    private String str = "距离";//根据什么排序
    private int checkPostiton;//左侧选中那个位置
    private int allPage;

    @Override
    protected void initView() {
        tvSearch.setText("搜索商品、超市");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        marketRlv.setLayoutManager(manager);
        marketRlv.setRefreshEnabled(true);
        marketRlv.setLoadMoreEnabled(true);
        marketRlv.setLoadingListener(this);
        marketAdapter = new SuperMaketAdapter(this, list);
        marketRlv.setAdapter(marketAdapter);
        adapter = new LeftListAdapter(this, leftList, flagArray);
        View rightHeader = LayoutInflater.from(this).inflate(R.layout.left_header_item, null);
        leftListview.addHeaderView(rightHeader, null, false);
        leftListview.setAdapter(adapter);
        marketPresenter = new MarketPresenter(this);
        superMarketPresenter = new SuperMarketPresenter(this);
        utils = SpfUtils.getSpfUtils(this);
        gouwucheNumber.setText(utils.getGwcNumber() + "");
        clickListener();
    }

    private void clickListener() {
        //左边分类
        leftListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
                for (int i = 0; i < leftList.size(); i++) {
                    if (i == position - 1) {
                        flagArray.add(i, true);
                    } else {
                        flagArray.add(i, false);
                    }
                }
                checkPostiton = position - 1;
                marketRlv.setNoMore(false);
                page = 0;
                superMarketPresenter.getSuperMarketData(str, utils.getLatitude(), utils.getLongitude(), leftList.get(position - 1), page);
                adapter.notifyDataSetChanged();
            }
        });
        //右边超市
        marketAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent i = new Intent(SuperMarketActivity.this, MarketActivity.class);
                i.putExtra("shopInfoId", list.get(position).getShopInfoId());
                i.putExtra("supType", list.get(position).getLeftMarketType());
                startActivity(i);
            }
        });
    }

    @Override
    protected void initData() {
        marketPresenter.getMarketData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.super_market_layout;
    }

    @OnClick({R.id.market_back, R.id.market_gouwuche, R.id.market_search, R.id.tv_distance, R.id.tv_haoping})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.market_back://返回
                finish();
                break;
            case R.id.market_gouwuche://购物车
                Intent intent = new Intent(SuperMarketActivity.this, MainActivity.class);
                intent.putExtra("type", "购物车");
                startActivity(intent);
                finish();
                break;
            case R.id.market_search://搜索
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case R.id.tv_haoping://好评度
                tvHaoping.setTextColor(getResources().getColor(R.color.colorWite));
                tvHaoping.setBackgroundResource(R.drawable.market_drawable1);
                tvDistance.setTextColor(getResources().getColor(R.color.color000));
                tvDistance.setBackgroundResource(R.drawable.market_drawable);
                str = "好评度";
                page = 0;
                marketRlv.setNoMore(false);
                superMarketPresenter.getSuperMarketData(str, utils.getLatitude(), utils.getLongitude(), leftList.get(checkPostiton), page);
                break;
            case R.id.tv_distance://距离
                tvHaoping.setTextColor(getResources().getColor(R.color.color000));
                tvHaoping.setBackgroundResource(R.drawable.market_drawable);
                tvDistance.setTextColor(getResources().getColor(R.color.colorWite));
                tvDistance.setBackgroundResource(R.drawable.market_drawable1);
                str = "距离";
                page = 0;
                marketRlv.setNoMore(false);
                superMarketPresenter.getSuperMarketData(str, utils.getLatitude(), utils.getLongitude(), leftList.get(checkPostiton), page);
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getSuperMarketData(SuperMarketBean superMarketBean) {
        if (superMarketBean.getState() == 1) {
            allPage = superMarketBean.getData().getAllPage();
            if (page == 0) {
                list.clear();
            }
            list.addAll(superMarketBean.getData().getMarket());
            if (list.size() > 0) {
                marketRlv.setVisibility(View.VISIBLE);
                ivNoDianpu.setVisibility(View.GONE);
            } else {
                marketRlv.setVisibility(View.GONE);
                ivNoDianpu.setVisibility(View.VISIBLE);
            }
            marketAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getMarketData(MarketFenleiBean marketFenleiBean) {
        if (marketFenleiBean.getState() == 1) {
            leftList.clear();
            leftList.addAll(marketFenleiBean.getData());
            for (int i = 0; i < marketFenleiBean.getData().size(); i++) {
                if (i == 0) {
                    flagArray.add(i, true);
                } else {
                    flagArray.add(i, false);
                }
            }
            adapter.notifyDataSetChanged();
            superMarketPresenter.getSuperMarketData(str, utils.getLatitude(), utils.getLongitude(), leftList.get(0), page);
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        superMarketPresenter.getSuperMarketData(str, utils.getLatitude(), utils.getLongitude(), leftList.get(checkPostiton), page);
        marketRlv.completeRefresh();
    }


    @Override
    public void onLoadMore() {
        if (page >= allPage - 1) {
            marketRlv.setNoMore(true);
            return;
        }
        page++;
        superMarketPresenter.getSuperMarketData(str, utils.getLatitude(), utils.getLongitude(), leftList.get(checkPostiton), page);
        marketRlv.completeLoadMore();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (marketPresenter != null) {
            marketPresenter = null;
        }
    }
}
